package com.googlecode.yami;

import java.sql.Connection;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class ConnectionDialog {

    public static Connection getConnection() {

	Connection validConnection = null;
	boolean repeatConnection = true;

	while (repeatConnection) {
	    JFrame main = new JFrame();
	    JLabel Spec_hostName = new JLabel("Host:");
	    JTextField host = new JTextField();
	    JLabel Spec_userName = new JLabel("User name:");
	    JTextField userName = new JTextField();
	    JLabel Spec_password = new JLabel("Password:");
	    JPasswordField password = new JPasswordField();

	    Object[] inputs = new Object[] { Spec_hostName, host,
		    Spec_userName, userName, Spec_password, password };

	    // Option frame for user and password
	    int result = JOptionPane.showOptionDialog(main, inputs,
		    "Login - YAMI " + MainWindow.VERSION,
		    JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
		    null, null, null);
		    
	    /* testing only
	    int result = JOptionPane.OK_OPTION;
	    host.setText("localhost");
	    userName.setText("root");
	    password.setText("os88");
	    */
	    
	    if (result == JOptionPane.OK_OPTION) {
		validConnection = Connect.login(host.getText(), userName
			.getText(), new String(password.getPassword()),
			MainWindow.DEFAULT_HOST);
		if (validConnection != null) {
		    repeatConnection = false;
		    return validConnection;
		} else {
		    repeatConnection = true;
		}
	    } else {
		validConnection = null;
		repeatConnection = false;
		System.exit(0);
	    }
	}
	return validConnection;
    }
}
